using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Admin_Admin_CouponInfo : Admin, IRequiresSessionState
{
	protected HtmlTable table_item;
	protected GridView gvList;
	protected Label PagePanel;
	protected Button ButDel;
	protected PlaceHolder phManage;
	protected Literal ltrTitle;
	protected TextBox txtCouponName;
	protected TextBox txtCouponNumPattern;
	protected TextBox txtCouponParvalue;
	protected TextBox txtNeedsMoney;
	protected TextBox txtBeginDate;
	protected TextBox txtEndDate;
	protected CheckBoxList cblUserGroup;
	protected RadioButtonList rblGetFashion;
	protected HtmlTable AddTable;
	protected Literal ltrCheckTitle;
	protected Literal ltrCouponName;
	protected Literal ltrCouponNumPattern;
	protected Literal ltrCouponParvalue;
	protected Literal ltrNeedsMoney;
	protected Literal ltrBeginDate;
	protected Literal ltrEndDate;
	protected Literal ltrUserGroup;
	protected Literal ltrGetFashion;
	protected HtmlTable CheckTable;
	protected HtmlAnchor addBuyClassify;
	protected HtmlGenericControl divClass;
	protected HtmlAnchor addBuySpecific;
	protected HtmlGenericControl divProduct;
	protected TextBox txtOrderMoney;
	protected RadioButtonList rblUseProductType;
	protected HtmlTable AddTableLimit;
	protected Literal ltrUseProductType;
	protected HtmlTable CheckTableLimit;
	protected HtmlAnchor addLimitClassify;
	protected HtmlGenericControl divLimitClassify;
	protected HtmlAnchor addLimitSpecific;
	protected HtmlGenericControl divLimitSpecific;
	protected HtmlInputCheckBox chkCouponIsPublish;
	protected HtmlTable AddTablePublisth;
	protected Literal ltrCouponIsPublish;
	protected HtmlTable CheckTablePublisth;
	protected TextBox txtCouponCount;
	protected Literal ltrCouponCount;
	protected HiddenField hfBuyClassifyID;
	protected HiddenField hfBuySpecificID;
	protected HiddenField hfFashionnContent;
	protected HiddenField hfLimitClassifyID;
	protected HiddenField hfLimitSpecificID;
	protected HiddenField hfLimitProductContent;
	protected Button btnAdd;
	protected PlaceHolder phAddOrEdit;
	protected Literal ltrPublistTitle;
	protected Literal ltrName;
	protected Literal ltrPattern;
	protected Literal ltrValue;
	protected TextBox txtPubCount;
	protected Button btnEdit;
	protected PlaceHolder phPublish;
	private string actionTab = "";
	private int infoID = 0;
	private HoWave.Data.Bll.CouponInfo bllCouponInfo;
	private HoWave.Data.Bll.CouponItem bllCouponItem;
	private string linkhead = "";
	private string beforeurl = "Admin_CouponInfo.aspx?MenuName=运营管理&FileName=优惠券规则管理";
	public string strPCF = "";
	public string strPCL = "";
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public string ActionTab
	{
		get
		{
			if (base.Request.QueryString["action"] != null)
			{
				this.actionTab = base.Request.QueryString["action"].Trim();
			}
			else
			{
				this.actionTab = "";
			}
			return this.actionTab;
		}
		set
		{
			this.actionTab = value;
		}
	}
	public int InfoID
	{
		get
		{
			if (base.Request.QueryString["InfoID"] != null)
			{
				this.infoID = Convert.ToInt32(base.Request.QueryString["InfoID"].Trim());
			}
			return this.infoID;
		}
	}
	public HoWave.Data.Bll.CouponInfo BllCouponInfo
	{
		get
		{
			if (this.bllCouponInfo == null)
			{
				this.bllCouponInfo = new HoWave.Data.Bll.CouponInfo();
			}
			return this.bllCouponInfo;
		}
	}
	public HoWave.Data.Bll.CouponItem BllCouponItem
	{
		get
		{
			if (this.bllCouponItem == null)
			{
				this.bllCouponItem = new HoWave.Data.Bll.CouponItem();
			}
			return this.bllCouponItem;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.admin.CheckAdminPower(42))
			{
				base.WriteLog("Modidy", Language.GetAdminMsg("Log/Access", "0"), "Admin_CouponInfo");
				base.WriteRightErrMsg();
			}
			this.ShowPanelDisplay(this.ActionTab);
			this.rblGetFashion.Attributes.Add("onclick", "ControlFashion();");
			this.rblUseProductType.Attributes.Add("onclick", "ControlUseProductType();");
			this.addBuyClassify.Attributes.Add("onclick", "SelectClassify('" + this.hfBuyClassifyID.ClientID + "');");
			this.addBuySpecific.Attributes.Add("onclick", "SelectProduct('" + this.hfBuySpecificID.ClientID + "');");
			this.addLimitClassify.Attributes.Add("onclick", "SelectClassifyL('" + this.hfLimitClassifyID.ClientID + "');");
			this.addLimitSpecific.Attributes.Add("onclick", "SelectProductL('" + this.hfLimitSpecificID.ClientID + "');");
			GroupBll groupBll = new GroupBll(this.conn);
			this.cblUserGroup.DataSource = groupBll.GetGroupsNameID();
			this.cblUserGroup.DataBind();
			if (this.InfoID > 0)
			{
				this.InitPage();
			}
		}
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (DisplayName != null)
		{
			if (DisplayName == "addinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.table_item.Visible = false;
				this.phPublish.Visible = false;
				this.ltrTitle.Text = "添加优惠券规则";
				this.btnAdd.Text = "添加";
				this.AddTable.Style["display"] = "block";
				this.CheckTable.Style["display"] = "none";
				this.AddTableLimit.Style["display"] = "block";
				this.CheckTableLimit.Style["display"] = "none";
				this.AddTablePublisth.Style["display"] = "block";
				this.CheckTablePublisth.Style["display"] = "none";
				this.ltrCouponCount.Visible = false;
				this.txtCouponNumPattern.Text = "?#################??";
				return;
			}
			if (DisplayName == "editinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.table_item.Visible = false;
				this.phPublish.Visible = false;
				this.ltrTitle.Text = "修改优惠券规则";
				this.btnAdd.Text = "修改";
				this.AddTable.Style["display"] = "block";
				this.CheckTable.Style["display"] = "none";
				this.AddTableLimit.Style["display"] = "block";
				this.CheckTableLimit.Style["display"] = "none";
				this.AddTablePublisth.Style["display"] = "block";
				this.CheckTablePublisth.Style["display"] = "none";
				this.ltrCouponCount.Visible = false;
				return;
			}
			if (DisplayName == "checkinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.table_item.Visible = false;
				this.phPublish.Visible = false;
				this.ltrCheckTitle.Text = "查看优惠券规则";
				this.btnAdd.Style["display"] = "none";
				this.AddTable.Style["display"] = "none";
				this.CheckTable.Style["display"] = "";
				this.AddTableLimit.Style["display"] = "none";
				this.CheckTableLimit.Style["display"] = "";
				this.AddTablePublisth.Style["display"] = "none";
				this.CheckTablePublisth.Style["display"] = "";
				this.addBuyClassify.Style["display"] = "none";
				this.addBuySpecific.Style["display"] = "none";
				this.addLimitClassify.Style["display"] = "none";
				this.addLimitSpecific.Style["display"] = "none";
				this.txtCouponCount.Style["display"] = "none";
				return;
			}
			if (DisplayName == "editpublish")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = false;
				this.table_item.Visible = false;
				this.phPublish.Visible = true;
				return;
			}
		}
		this.phManage.Visible = true;
		this.phAddOrEdit.Visible = false;
		this.table_item.Visible = true;
		this.phPublish.Visible = false;
		this.BindList();
	}
	protected void BindList()
	{
		CurrPageLinks currPageLinks = new CurrPageLinks();
		string text = this.QuestString();
		string str = "order by CouponInfoID desc";
		int num = 0;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		int pageSize = this.gvList.PageSize;
		ClientByPage clientByPage = new ClientByPage();
		string text2 = " from HL_CouponInfo ";
		int listCount = clientByPage.GetListCount("select count(*)" + text2 + text);
		string url = this.beforeurl + this.linkhead + "&pageindex";
		int startIndex = (num - 1) * pageSize + 1;
		int endIndex = num * pageSize;
		string str2 = "ROW_NUMBER() OVER (" + str + ") as row,* ";
		string strSQL = "select " + str2 + text2 + text;
		DataTable listPage = clientByPage.GetListPage(strSQL, "row", startIndex, endIndex);
		this.gvList.DataSource = listPage;
		this.gvList.DataBind();
		if (listPage.Rows.Count > 0)
		{
			this.PagePanel.Text = currPageLinks.CurrPageLink(num, pageSize, listCount, "条记录&nbsp;&nbsp;每页" + pageSize + "条数据&nbsp;&nbsp;", url);
		}
		for (int i = 0; i < this.gvList.Rows.Count; i++)
		{
			HiddenField hiddenField = (HiddenField)this.gvList.Rows[i].FindControl("hfCouponIsPublish");
			LinkButton linkButton = (LinkButton)this.gvList.Rows[i].FindControl("btnEdit");
			if (hiddenField != null)
			{
				linkButton.Visible = true;
				if (hiddenField.Value.ToString() == "1" || hiddenField.Value.ToString().ToLower() == "true")
				{
					linkButton.Visible = false;
				}
			}
		}
	}
	public string getUserGroupName(object value)
	{
		string text = "";
		if (value != null && value.ToString() != "")
		{
			GroupBll groupBll = new GroupBll(this.conn);
			DataTable groupsNameByID = groupBll.GetGroupsNameByID(value.ToString());
			for (int i = 0; i < groupsNameByID.Rows.Count; i++)
			{
				text = text + groupsNameByID.Rows[i]["GroupName"].ToString() + ",";
			}
			if (text != "")
			{
				text = text.Substring(0, text.Length - 1);
			}
		}
		return text;
	}
	public string getPublist(object value)
	{
		string result = "";
		if (value != null)
		{
			if (value.ToString() == "1" || value.ToString().ToLower() == "true")
			{
				result = "<span style='color:Green' >√</span>";
			}
			else
			{
				result = "<span style='color:Red' >×</span>";
			}
		}
		return result;
	}
	protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
		}
	}
	protected string QuestString()
	{
		return "";
	}
	protected void Button_Command(object sender, CommandEventArgs e)
	{
		string commandName = e.CommandName;
		if (commandName == "MoreDel")
		{
			string text = Text.CheckNumList(Fetch.GetOrPost("chk_select"));
			if (text != "")
			{
				bool flag = false;
				string[] array = text.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					flag = this.delInfo(array[i]);
				}
				if (flag)
				{
					this.WriteErrMsg("", "批量信息删除成功！", this.beforeurl + this.linkhead + "&pageindex");
				}
			}
			else
			{
				this.WriteErrMsg("", "请选您要删除的信息", "");
			}
		}
		else
		{
			if (commandName == "DelInfo")
			{
				string text2 = e.CommandArgument.ToString();
				bool flag2 = this.delInfo(text2);
				if (flag2)
				{
					this.WriteErrMsg("", "删除成功！", this.beforeurl + this.linkhead + "&pageindex");
				}
			}
			else
			{
				if (commandName == "EditInfo")
				{
					string text2 = e.CommandArgument.ToString();
					if (!string.IsNullOrEmpty(text2))
					{
						base.Response.Redirect("Admin_CouponInfo.aspx?MenuName=运营管理&FileName=发布优惠券&action=editpublish&InfoID=" + text2);
					}
				}
			}
		}
		this.BindList();
	}
	private bool delInfo(string id)
	{
		bool result = false;
		HoWave.Data.Model.CouponInfo model = this.BllCouponInfo.GetModel(Convert.ToInt32(id));
		if (model != null)
		{
			DataTable dataTable = this.BllCouponItem.GetList("CouponInfoID=" + id).Tables[0];
			bool flag;
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				int couponItemID = Convert.ToInt32(dataTable.Rows[i]["CouponItemID"].ToString());
				flag = this.BllCouponItem.Delete(couponItemID);
				if (flag)
				{
					string sql = "update HL_CouponInfo set CouponCount=CouponCount-1 where CouponInfoID=" + Convert.ToInt32(id);
					this.conn.Execute(sql);
				}
			}
			flag = this.BllCouponInfo.Delete(Convert.ToInt32(id));
		}
		return result;
	}
	protected void InitPage()
	{
		HoWave.Data.Model.CouponInfo model = this.BllCouponInfo.GetModel(this.InfoID);
		if (model != null)
		{
			string text = this.ActionTab;
			if (text != null)
			{
				if (!(text == "editinfo"))
				{
					if (!(text == "checkinfo"))
					{
						if (text == "editpublish")
						{
							this.ltrName.Text = model.CouponName;
							this.ltrPattern.Text = model.CouponNumPattern;
							this.ltrValue.Text = model.CouponParvalue.ToString();
						}
					}
					else
					{
						this.ltrCouponName.Text = model.CouponName;
						this.ltrCouponNumPattern.Text = model.CouponNumPattern;
						this.ltrCouponParvalue.Text = model.CouponParvalue.ToString();
						this.ltrNeedsMoney.Text = model.NeedsMoney.ToString();
						this.ltrBeginDate.Text = Convert.ToDateTime(model.BeginDate).ToString("yyyy-MM-dd");
						this.ltrEndDate.Text = Convert.ToDateTime(model.EndDate).ToString("yyyy-MM-dd");
						this.ltrUserGroup.Text = this.getUserGroupName(model.UserGroupID);
						string text2 = "";
						text = model.GetFashion;
						if (text != null)
						{
							if (!(text == "BuyAny"))
							{
								if (!(text == "BuyClassify"))
								{
									if (!(text == "BuySpecific"))
									{
										if (text == "BuyOrderMoney")
										{
											text2 = "订单满足一定金额可得到此优惠券";
										}
									}
									else
									{
										text2 = "购买指定商品可得到此优惠券";
									}
								}
								else
								{
									text2 = "购买指定分类商品可得到此优惠券";
								}
							}
							else
							{
								text2 = "购买任意商品可得到此优惠券";
							}
						}
						this.ltrGetFashion.Text = text2;
						this.strPCF = model.GetFashionContent;
						string text3 = "";
						text = model.UseProductType;
						if (text != null)
						{
							if (!(text == "LimitAll"))
							{
								if (!(text == "LimitClassify"))
								{
									if (text == "LimitSpecific")
									{
										text3 = "指定商品";
									}
								}
								else
								{
									text3 = "指定分类";
								}
							}
							else
							{
								text3 = "所有商品";
							}
						}
						this.ltrUseProductType.Text = text3;
						this.strPCL = model.UseProductIDs;
						this.txtCouponCount.Text = model.CouponCount.ToString();
						string text4 = model.CouponIsPublish.ToString();
						if (text4 == "1" || text4.ToLower() == "true")
						{
							this.ltrCouponIsPublish.Text = "是";
						}
						else
						{
							this.ltrCouponIsPublish.Text = "否";
						}
						if (this.ActionTab == "checkinfo")
						{
							this.ltrCouponCount.Visible = true;
							this.ltrCouponCount.Text = model.CouponCount.ToString() + "张";
						}
					}
				}
				else
				{
					this.txtCouponName.Text = model.CouponName;
					this.txtCouponNumPattern.Text = model.CouponNumPattern;
					this.txtCouponParvalue.Text = model.CouponParvalue.ToString();
					this.txtCouponParvalue.Enabled = false;
					this.txtNeedsMoney.Text = model.NeedsMoney.ToString();
					this.txtBeginDate.Text = Convert.ToDateTime(model.BeginDate).ToString("yyyy-MM-dd");
					this.txtEndDate.Text = Convert.ToDateTime(model.EndDate).ToString("yyyy-MM-dd");
					this.cblUserGroup.cblSelect(model.UserGroupID);
					this.rblGetFashion.rblSelect(model.GetFashion);
					this.strPCF = model.GetFashionContent;
					this.rblUseProductType.rblSelect(model.UseProductType);
					this.strPCL = model.UseProductIDs;
					this.txtCouponCount.Text = model.CouponCount.ToString();
					string text5 = model.CouponIsPublish.ToString();
					this.chkCouponIsPublish.Disabled = true;
					this.txtCouponCount.Enabled = false;
					if (text5 == "1" || text5.ToLower() == "true")
					{
						this.chkCouponIsPublish.Checked = true;
						this.txtCouponNumPattern.Enabled = false;
					}
				}
			}
		}
	}
	private HoWave.Data.Model.CouponInfo GetInfoModel()
	{
		HoWave.Data.Model.CouponInfo couponInfo = new HoWave.Data.Model.CouponInfo();
		string text = this.txtCouponName.Text;
		string text2 = this.txtCouponNumPattern.Text;
		string text3 = this.txtCouponParvalue.Text;
		string text4 = this.txtNeedsMoney.Text;
		string text5 = this.txtBeginDate.Text;
		string text6 = this.txtEndDate.Text;
		string userGroupID = this.cblUserGroup.cblSelectValue();
		string selectedValue = this.rblGetFashion.SelectedValue;
		string selectedValue2 = this.rblUseProductType.SelectedValue;
		int value = 0;
		bool couponIsPublish = false;
		if (this.chkCouponIsPublish.Checked)
		{
			value = Convert.ToInt32(this.txtCouponCount.Text);
			couponIsPublish = true;
		}
		couponInfo.CouponName = text;
		couponInfo.CouponNumPattern = text2;
		couponInfo.CouponParvalue = new decimal?(Convert.ToDecimal(text3));
		couponInfo.NeedsMoney = new decimal?(Convert.ToDecimal(text4));
		couponInfo.BeginDate = new DateTime?(Convert.ToDateTime(text5));
		couponInfo.EndDate = new DateTime?(Convert.ToDateTime(text6));
		couponInfo.UserGroupID = userGroupID;
		couponInfo.GetFashion = selectedValue;
		couponInfo.UseProductType = selectedValue2;
		couponInfo.CouponCount = new int?(value);
		couponInfo.CouponIsPublish = couponIsPublish;
		return couponInfo;
	}
	private bool CheckInput()
	{
		bool result = true;
		string text = this.txtBeginDate.Text;
		string text2 = this.txtEndDate.Text;
		if (!string.IsNullOrEmpty(text2))
		{
			if (!string.IsNullOrEmpty(text))
			{
				if (Convert.ToDateTime(text) > Convert.ToDateTime(text2))
				{
					MessageBox.Show(this, Language.Get("Common/DateTimeErr"));
					result = false;
				}
			}
		}
		return result;
	}
	protected void btnAdd_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			string text = this.ActionTab;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (text == "editinfo")
					{
						HoWave.Data.Model.CouponInfo couponInfo = this.BllCouponInfo.GetModel(this.InfoID);
						couponInfo = this.GetInfoModel();
						couponInfo.CouponInfoID = this.InfoID;
						couponInfo.GetFashionContent = this.hfFashionnContent.Value;
						couponInfo.UseProductIDs = this.hfLimitProductContent.Value;
						bool flag = this.BllCouponInfo.Update(couponInfo);
						if (flag)
						{
							this.WriteSuccessMsg("", "优惠券规则信息修改成功！", this.beforeurl);
						}
						else
						{
							this.WriteErrMsg("", "优惠券规则信息修改失败！", "");
						}
					}
				}
				else
				{
					HoWave.Data.Model.CouponInfo infoModel = this.GetInfoModel();
					infoModel.GetFashionContent = this.hfFashionnContent.Value;
					infoModel.UseProductIDs = this.hfLimitProductContent.Value;
					int num = this.BllCouponInfo.Add(infoModel);
					if (num > 0)
					{
						if (this.chkCouponIsPublish.Checked)
						{
							int num2 = this.ModelItem(num, this.txtCouponNumPattern.Text, Convert.ToInt32(this.txtCouponCount.Text));
							if (num2 > 0)
							{
								this.WriteSuccessMsg("", "优惠券规则信息添加成功！", this.beforeurl);
							}
						}
						else
						{
							this.WriteSuccessMsg("", "优惠券规则信息添加成功！", this.beforeurl);
						}
					}
					else
					{
						this.WriteErrMsg("", "优惠券规则信息添加失败！", "");
					}
				}
			}
		}
	}
	private int ModelItem(int couponinfoid, string pattern, int couponcount)
	{
		int result = 0;
		Random random = new Random(DateTime.Now.Millisecond);
		for (int i = 0; i < couponcount; i++)
		{
			HoWave.Data.Model.CouponItem couponItem = new HoWave.Data.Model.CouponItem();
			int num = pattern.IndexOf("#################");
			if (num > 0)
			{
				string text = pattern.Substring(0, num);
				string text2 = pattern.Substring(num, 17);
				int num2 = num + 17;
				string text3 = pattern.Substring(num2, pattern.Length - num2);
				string[] array = StaticFun.RandCode(num, couponcount).Split(new char[]
				{
					','
				});
				string[] array2 = StaticFun.RandCode(pattern.Length - num2, couponcount).Split(new char[]
				{
					','
				});
				string newValue = DateTime.Now.ToString("yyyyMMddHHmmss") + random.Next(100, 999);
				string str = text.Replace(text, array[i]);
				string str2 = text2.Replace(text2, newValue);
				string str3 = text3.Replace(text3, array2[i]);
				string couponNum = str + str2 + str3;
				couponItem.CouponInfoID = new int?(couponinfoid);
				couponItem.CouponNum = couponNum;
				couponItem.UseStatus = false;
				couponItem.EmailStatus = false;
				couponItem.MobileStatus = false;
				couponItem.MessageStatus = false;
				result = this.BllCouponItem.Add(couponItem);
			}
		}
		return result;
	}
	protected void btnEdit_Click(object sender, EventArgs e)
	{
		HoWave.Data.Model.CouponInfo model = this.BllCouponInfo.GetModel(this.InfoID);
		if (model != null)
		{
			model.CouponIsPublish = true;
			model.CouponInfoID = this.InfoID;
			model.CouponCount = new int?(Convert.ToInt32(this.txtPubCount.Text));
			bool flag = this.BllCouponInfo.Update(model);
			if (flag)
			{
				int num = this.ModelItem(this.InfoID, this.ltrPattern.Text, Convert.ToInt32(this.txtPubCount.Text));
				this.WriteErrMsg("", "优惠券发布成功！", this.beforeurl + this.linkhead + "&pageindex");
			}
			else
			{
				this.WriteErrMsg("", "优惠券发布失败！", "");
			}
		}
	}
}
